﻿//
// GNS_DailyPlayDate.cpp
// MobileManage
//
// Created by MR.L on 16/2/17.
//
//
#include "GnsClientFrame.h"
#include "GNS_DailyPlayDate.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

//extern ISocketMgr * _GameSock;

GNS_DailyPlayDate::GNS_DailyPlayDate()
{
    pGNS_DailyPlayDate=0;
    DataListView=nullptr;
    pPlayName=nullptr;
    TimeText=nullptr;
    pCalendar=nullptr;
    TitleText=nullptr;
    GameType=nullptr;
    GameName=nullptr;
    Year=0;
    Mon=0;
    Day=0;
}

GNS_DailyPlayDate::~GNS_DailyPlayDate()
{
    
}

GNS_DailyPlayDate* GNS_DailyPlayDate::createLayer()
{
    auto pGNS_DailyPlayDate = GNS_DailyPlayDate::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_DailyPlayDate);
    
    return pGNS_DailyPlayDate;
}

bool GNS_DailyPlayDate::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;
    return true;
}
void GNS_DailyPlayDate::SetNodeName(std::string CsbName)
{
    pGNS_DailyPlayDate = CSLoader::createNode(CsbName);
    pGNS_DailyPlayDate->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_DailyPlayDate);
    addChild(pGNS_DailyPlayDate,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_DailyPlayDate);
    auto pBackBtn = static_cast<Button*>(pGNS_DailyPlayDate->getChildByName("PvPApplyTickBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyPlayDate::BtnCallBack, this));
    
    DataListView =static_cast<ListView*>(pGNS_DailyPlayDate->getChildByName("DateListView"));
    GameName = static_cast<Text*>(pGNS_DailyPlayDate->getChildByName("Text_3_0"));

    GameType = static_cast<ListView*>(pGNS_DailyPlayDate->getChildByName("GameType"));
    GameType->removeAllItems();

	int x = 0;
    for (auto it:(* GNS_PlayerMgr::getInstance()->getGameList())) {
        if (it.iGameType==2) {
            auto pbutton = Button::create("p0Btn.png");
			pbutton->setTitleText(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
            pbutton->setTitleColor(Color3B::BLACK);
            pbutton->setTitleFontSize(20);
            pbutton->setTag(7);
            pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_DailyPlayDate::BtnCallBack, this));
            GameType->addChild(pbutton);

			x++;
			if (x == 1)
				GameName->setString(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
        }
    }
    GameType->setVisible(false);

	if (GNS_PlayerMgr::getInstance()->getGameId() > 0)
	{
		GameName->setString(GNS_PlayerMgr::getInstance()->getGameIdInfo()[GNS_PlayerMgr::getInstance()->getGameId()]);
	}

    pPlayName = static_cast<Text*>(pGNS_DailyPlayDate->getChildByName("Text_4"));
    TimeText=static_cast<Text*>(pGNS_DailyPlayDate->getChildByName("TimeText"));
    TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));
    
    auto pTimeBtn = static_cast<Button*>(pGNS_DailyPlayDate->getChildByName("TimeBtn"));
    pTimeBtn->setTag(2);
	pTimeBtn->setVisible(false);
    //pTimeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_DailyPlayDate::BtnCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_DailyPlayDate->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    //pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_DailyPlayDate::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_DailyPlayDate->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(3);
    //typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_DailyPlayDate::BtnCallBack, this));
    
    auto gamebtn1 = static_cast<Button*>(pGNS_DailyPlayDate->getChildByName("GameBtn"));
    gamebtn1->setTag(3);
	gamebtn1->setVisible(false);
    //gamebtn1->addTouchEventListener(CC_CALLBACK_2(GNS_DailyPlayDate::BtnCallBack, this));
    
    TitleText = static_cast<Text*>(pGNS_DailyPlayDate->getChildByName("Text_5"));

	auto pve = static_cast<ImageView*>(pGNS_DailyPlayDate->getChildByName("PlayerPve"));
	auto pvp = static_cast<ImageView*>(pGNS_DailyPlayDate->getChildByName("PlayerPvp"));
	pvp->setVisible(true);
	pve->setVisible(false);

    AcceptSeverDate();
}
void GNS_DailyPlayDate::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                int vecSize = (int)GNS_PlayerMgr::getInstance()->_NodeChildVec.size();
                if (vecSize>0) {
                    if (DataListView != nullptr)
                        DataListView->removeAllItems();
					GNS_PlayerMgr::getInstance()->getGameReveRes()->clear();
                    GNS_PlayerMgr::getInstance()->getGameProReveRes()->clear();
                    CMD_CS_GetManToManGameRevenue  pGetGameReven;
                    pGetGameReven._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
					if (!GNS_PlayerMgr::getInstance()->getUpToLowCode().empty())
					{
						pGetGameReven._uplineCode = GNS_PlayerMgr::getInstance()->getUpToLowCode()[CurID];
					}
                    pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameId();
                    pGetGameReven._type=2;
                    std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                    memcpy(pGetGameReven._selDate, TimeDate.c_str(), 11);
					GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
                    GameType->setVisible(false);
					ClearProName();
                    
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.erase(vecSize-1);
                    this->removeAllChildrenWithCleanup(true);
                    //this->getParent()->removeChild(this);
                }
                if (vecSize >= 2)
                {
                    auto PromoterNode=GNS_PlayerMgr::getInstance()->_NodeChildVec.at(vecSize-2);
                    PromoterNode->setVisible(true);
                }
                break;
            }
            case 2:
            {
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_DailyPlayDate::TimeButtonCallBack, this));
				/*pCalendar =GNS_Calendar::createCalendar();
				pCalendar->SetCalendBgImage("Calendarbg.png");
				std::vector<Button *> CalenderBtn;
				pCalendar->GetTimeButton()->swap(CalenderBtn);
				for (auto & it:CalenderBtn) {
				it->setTag(100);
				it->addTouchEventListener(CC_CALLBACK_2(GNS_DailyPlayDate::TimeButtonCallBack, this));
				}*/
                break;
            }
            case 3:
            {
                if (GameType->isVisible()==true) {
                    GameType->setVisible(false);
                }else{
                    GameType->setVisible(true);
                }
                break;
            }
            case 7:
            {
                GameName->setString(pButton->getTitleText());
                for (int i =0; i<GNS_PlayerMgr::getInstance()->getGameList()->size(); i++) {
					auto st1 = SystemMgr::getInstance()->utf8ToGb(pButton->getTitleText().c_str());
					auto st2 = GNS_PlayerMgr::getInstance()->getGameList()->at(i).szGameName;
                     if (st1 == st2) {
                         if (DataListView != nullptr)
                             DataListView->removeAllItems();
                            CMD_CS_GetManToManGameRevenue  pGetGameReven;
                            pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                            pGetGameReven._uplineCode=CurID;
                            pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameList()->at(i).nGameId;
                            pGetGameReven._type=3;
                            std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                            memcpy(pGetGameReven._selDate, TimeDate.c_str(), 11);
							GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
							GameType->setVisible(false);
							GNS_PlayerMgr::getInstance()->setGameId(pGetGameReven._gameId);
                     }
                    GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->clear();
                }
                break;
            }
            default:
                break;
        }
    }
}
void GNS_DailyPlayDate::AcceptSeverDate()
{
    int belongType;
    DataListView->removeAllItems();
    int index =(int)GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->size();
    Size bgSize;
    if (index > 0)
    {
        GNS_PlayerMgr::getInstance()->transferEachTime(Year,Mon, Day, GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->at(0)._selDate);
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));
        GameName->setString(GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->at(0)._gameName);
    }
    for (auto it:(*GNS_PlayerMgr::getInstance()->getGamePlayReveRes())) {
        auto Itembg = ImageView::create("line2.png");
        DataListView->addChild(Itembg);
        bgSize = Itembg->getContentSize();
        
        auto Name = Text::create(StringUtils::format("%d",it._lowlineCode), GameFontName, 24);
        Name->setPosition(Vec2(bgSize.width*0.14, bgSize.height*0.34));
        Name->setColor(Color3B::BLACK);
        Itembg->addChild(Name);
        
        auto lineRevenueRatio = Text::create(StringUtils::format("%lld",it._lowlineRevenueRatio), GameFontName, 24);
        lineRevenueRatio->setPosition(Vec2(bgSize.width*0.45, bgSize.height*0.34));
        lineRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(lineRevenueRatio);
        
        auto agentRevenueRatio = Text::create(StringUtils::format("%lld",it._agentRevenueRatio), GameFontName, 24);
        agentRevenueRatio->setPosition(Vec2(bgSize.width*0.8, bgSize.height*0.34));
        agentRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(agentRevenueRatio);
        
        belongType= it._belongType;
    }
    
    if (bgSize.width!=0||bgSize.height!=0) {
        DataListView->setInnerContainerSize(Size(bgSize.width, (bgSize.height+10)*index));
    }
    if (belongType==21) {
        TitleText->setString("推广的玩家税收分成");
    }
}
void GNS_DailyPlayDate::SetProName(int agentCode)
{
	if (pPlayName != nullptr)
	{
		std::string strAgentCode = CCGnsOften::getUtf8_DirectAssignmentInProgram("推广员账号:");
		strAgentCode.append(SystemMgr::getInstance()->GbToUTF8(StringUtils::format("%d", agentCode).c_str()));
		pPlayName->setString(strAgentCode);
	}
	CurID = agentCode;
	//std::string strTip = StringUtils::format("推广员账号:%d", agentCode);//推广员账号:
	//pPlayName->setString(strTip);
	//CurID = agentCode;
}
void GNS_DailyPlayDate::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        Mon = pCalendar->Mon;
        Year = pCalendar->year;
        Day = atoi(BtnText.c_str());
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        if (DataListView != nullptr)
            DataListView->removeAllItems();
        GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->clear();
        CMD_CS_GetManToManGameRevenue  pGetGameReven;
        pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
		pGetGameReven._uplineCode = CurID;
        pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameId();
        pGetGameReven._type=3;
        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetGameReven._selDate, TimeDate.c_str(), 11);
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
    }
}